﻿using System;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.POS;
using Me.Amon.POS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdateEmp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var item = new ListItem { Text = "导购员", Value = CEmp.ACTOR_3.ToString() };
            LbActor.Items.Add(item);
            CbActor.Items.Add(item);
            item = new ListItem { Text = "收银员", Value = CEmp.ACTOR_4.ToString() };
            LbActor.Items.Add(item);
            CbActor.Items.Add(item);
            item = new ListItem { Text = "账务员", Value = CEmp.ACTOR_5.ToString() };
            LbActor.Items.Add(item);
            CbActor.Items.Add(item);
            item = new ListItem { Text = "店长", Value = CEmp.ACTOR_6.ToString() };
            LbActor.Items.Add(item);
            CbActor.Items.Add(item);
            item = new ListItem { Text = "技术员", Value = CEmp.ACTOR_8.ToString() };
            LbActor.Items.Add(item);
            CbActor.Items.Add(item);

            RbSex.Items.Add(new ListItem { Text = "男", Value = CEmp.SEX_MALE.ToString() });
            RbSex.Items.Add(new ListItem { Text = "女", Value = CEmp.SEX_FEMALE.ToString() });
            RbSex.SelectedIndex = 0;

            TbAddr.MaxLength = CEmp.ADDRESS;
            TbCellPhone.MaxLength = CEmp.CELLPHONE;
            TbFirstName.MaxLength = CEmp.FIRST_NAME;
            TbLastName.MaxLength = CEmp.LAST_NAME;
            TbTelephone.MaxLength = CEmp.TELEPHONE;
            TbUser.MaxLength = CEmp.USER;
            TbUid.MaxLength = CEmp.UID;

            var act = Request["act"];
            if (!CharUtil.IsValidateHash(act))
            {
                act = CEmp.ACTOR_3.ToString();
            }
            LbActor.SelectedValue = act;

            var emp = Request["emp"];
            if (CharUtil.IsValidateHash(emp))
            {
                var dba = new RDBEngine();
                ReadEmp(dba, user.CorpId, emp);
            }
        }

        #region 事件处理
        protected void BtUpdateVip_Click(object sender, EventArgs e)
        {
            SaveEmp();
        }

        protected void BtCancelVip_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/Emps.aspx?act=" + LbActor.SelectedValue);
        }
        #endregion

        private void ReadEmp(RDBEngine dba, string corp, string vip)
        {
            dba.AddTable(DEmp.TABLE);
            dba.AddWhere(DEmp.CORP_ID, corp);
            dba.AddWhere(DEmp.ID, vip);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HdId.Value = vip;
            TbCode.Text = "" + row[DEmp.CODE];
            TbLastName.Text = "" + row[DEmp.LAST_NAME];
            TbFirstName.Text = "" + row[DEmp.FIRST_NAME];
            RbSex.SelectedValue = "" + row[DEmp.SEX];
            TbUser.Text = "" + row[DEmp.USER];
            TbPass.Text = "" + row[DEmp.PASS];

            CbActor.SelectedValue = "" + row[DEmp.ACTOR];
            TbBirthday.Text = "" + row[DEmp.BIRTHDAY];
            TbUid.Text = "" + row[DEmp.UID];
            TbEngageDate.Text = "" + row[DEmp.ENGAGE_DATE];
            TbResignDate.Text = "" + row[DEmp.RESIGN_DATE];
            TbCellPhone.Text = "" + row[DEmp.CELLPHONE];
            TbTelephone.Text = "" + row[DEmp.TELEPHONE];
            TbAddr.Text = "" + row[DEmp.ADDRESS];
            CkStatus.Checked = (CEmp.STATUS_DISABLED == (int)row[DEmp.STATUS]);
        }

        private void SaveEmp()
        {
            LlErr.Text = "";
            var id = HdId.Value;

            var lastName = TbLastName.Text.Trim();
            if (!WebUtil.IsValid(lastName, "姓", CEmp.LAST_NAME, LlErr))
            {
                return;
            }
            var firstName = TbFirstName.Text.Trim();
            if (!WebUtil.IsValid(firstName, "名", CEmp.FIRST_NAME, LlErr))
            {
                return;
            }

            var sex = RbSex.SelectedValue;
            if (string.IsNullOrWhiteSpace(sex))
            {
                LlErr.Text = "请选择员工性别！";
                return;
            }

            var uid = TbUid.Text.Trim();
            if (!WebUtil.IsValid(uid, "证件号码", CEmp.UID, LlErr))
            {
                return;
            }

            var userName = TbUser.Text.Trim();
            if (!WebUtil.IsValid(userName, "登录用户", CEmp.USER, LlErr))
            {
                return;
            }

            #region 检测用户的重复性
            var dba = new RDBEngine();
            dba.AddTable(DEmp.TABLE);
            dba.AddColumn(DEmp.USER, "cnt");
            dba.AddWhere(DEmp.USER, userName);
            dba.AddWhere(DEmp.STATUS, CEmp.STATUE_ENABLED.ToString(), false);
            if (CharUtil.IsValidateHash(id))
            {
                dba.AddWhere(DEmp.ID, "!=", id, true);
            }
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 0)
            {
                LlErr.Text = string.Format("已存在名为 {0} 的登录用户！", userName);
                return;
            }
            #endregion

            var userPass = TbPass.Text.Trim();
            if (string.IsNullOrWhiteSpace(userPass))
            {
                LlErr.Text = "请输入 登录口令！";
                return;
            }

            var user = WebUtil.CurrentUser(Session);

            bool isUpdate = CharUtil.IsValidateHash(id);
            string code = "";
            if (!isUpdate)
            {
                dba.Reset();
                code = KeyUtil.NextEmpKey(dba, user.CorpId);
            }

            dba.Reset();
            dba.AddTable(DEmp.TABLE);
            dba.AddParam(DEmp.CORP_ID, user.CorpId);
            dba.AddParam(DEmp.CHANNEL, CEmp.CHANNEL);
            dba.AddParam(DEmp.ACTOR, CbActor.SelectedValue, false);
            dba.AddParam(DEmp.LAST_NAME, lastName);
            dba.AddParam(DEmp.FIRST_NAME, firstName);
            dba.AddParam(DEmp.DISPLAY_NAME, lastName + firstName);
            dba.AddParam(DEmp.SEX, RbSex.SelectedValue, false);
            dba.AddParam(DEmp.USER, userName);
            dba.AddParam(DEmp.PASS, HashUtil.Password(userPass));
            dba.AddParam(DEmp.BIRTHDAY, TbBirthday.Text.Trim());
            dba.AddParam(DEmp.UID, uid);
            dba.AddParam(DEmp.CELLPHONE, TbCellPhone.Text.Trim());
            dba.AddParam(DEmp.TELEPHONE, TbTelephone.Text.Trim());
            dba.AddParam(DEmp.PROV, "");
            dba.AddParam(DEmp.CITY, "");
            dba.AddParam(DEmp.AREA, "");
            dba.AddParam(DEmp.ADDRESS, TbAddr.Text.Trim());
            dba.AddParam(DEmp.POSTCODE, "");
            dba.AddParam(DEmp.ENGAGE_DATE, TbEngageDate.Text.Trim());
            dba.AddParam(DEmp.RESIGN_DATE, TbResignDate.Text.Trim());
            dba.AddParam(DEmp.STATUS, CkStatus.Checked ? CEmp.STATUS_DISABLED : CEmp.STATUE_ENABLED);
            dba.AddParam(DEmp.UPDATE_TIME, DEmp.NOW, false);
            if (isUpdate)
            {
                dba.AddWhere(DEmp.ID, id);
                dba.ExecuteUpdate();

                Response.Redirect("~/POS/Emps.aspx?act=" + LbActor.SelectedValue);
            }
            else
            {
                id = HashUtil.UtcTimeInHex();
                dba.AddParam(DEmp.ID, id);
                dba.AddParam(DEmp.CODE, code);
                dba.AddParam(DEmp.CREATE_TIME, DEmp.NOW, false);
                dba.ExecuteInsert();

                ShowDef();
            }
        }

        private void ShowDef()
        {
            HdId.Value = "";
            TbCode.Text = "";
            TbLastName.Text = "";
            TbFirstName.Text = "";
            RbSex.SelectedIndex = 0;
            TbBirthday.Text = "";
            TbUser.Text = "";
            TbPass.Text = "";

            CbActor.SelectedIndex = 0;
            TbUid.Text = "";
            TbEngageDate.Text = "";
            TbResignDate.Text = "";
            TbCellPhone.Text = "";
            TbTelephone.Text = "";
            TbAddr.Text = "";
            CkStatus.Checked = false;
        }
    }
}